﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace Jquery.Validate.Additional
{
    /// <summary>
    /// 电话号码校验
    /// </summary>
    public class TelPhoneAttribute : RegularExpressionAttribute, IClientValidatable
    {
        private const string DefaultPattern = @"^0d{2}-d{7,8}|0d{3}-d{7,8}$";

        public TelPhoneAttribute()
            : base(DefaultPattern)
        {
            ErrorMessage = "电话号码格式错误";
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var rule = new ModelClientValidationRule
            {
                ErrorMessage = ErrorMessage,
                ValidationType = "telphone",
            };
            yield return rule;
        }

    }
}